﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WatiN.Core;
using WatiN.Core.Native;
using WatiN.Core.Logging;
using WatiN.Core.UtilityClasses;
using WatiN.Core.Properties;
using WatiN.Core.Native.Windows;
using WatiN.Core.DialogHandlers;
using WatiN.Core.Exceptions;
using WatiN.Core.Constraints;
using WatiN.Core.Comparers;
using System.Collections;
using mshtml;


namespace WatiNSahi
{
    public abstract partial class WatiNSahiTest
    {

        #region PRIVATE FUNCTIONS for support

        private static Constraint _get_constraint(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Constraint consNameOrIdOrTextOrLabelTextOrClassNameOrTitle = Find
            .ByName(nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
            .Or(Find.ById(nameOrIdOrTextOrLabelTextOrClassNameOrTitle))
            .Or(Find.ByText(nameOrIdOrTextOrLabelTextOrClassNameOrTitle))
            .Or(Find.ByLabelText(nameOrIdOrTextOrLabelTextOrClassNameOrTitle))
            .Or(Find.ByClass(nameOrIdOrTextOrLabelTextOrClassNameOrTitle))
            .Or(Find.ByTitle(nameOrIdOrTextOrLabelTextOrClassNameOrTitle));

            return consNameOrIdOrTextOrLabelTextOrClassNameOrTitle;
        }

        private static Constraint _get_constraint(string val, string attr)
        {
            return Find.By(attr,val);
        }

        #endregion


        #region EXPERIMENTAL



        private static Frame _getFrameFromParentFrame(string frameIdorName, Frame parentFrame)
        {
            return parentFrame.Frame(Find.ByName(frameIdorName).Or(Find.ById(frameIdorName)));
        }

        private static Frame _getFrameFromBrowser(string frameIdorName)
        {
            Frame frame = null;
            try
            {
                frame = browser.Frame(Find.ByName(frameIdorName).Or(Find.ById(frameIdorName)));
            }
            catch (FrameNotFoundException)
            { frame = null; }

            return frame;
        }



        public static Frame _frame(string nameOrId)
        {
            Frame f = _getFrameFromBrowser(nameOrId);
            if (f != null)
                return f;
            else
            {
                foreach (var frame in browser.Frames)
                {
                    _log(string.Format("Details of Frame => Name :: '{0}' ID :: '{1}' ", frame.Name, frame.Id));
                    return _frameFrame(nameOrId, frame);

                }
            }

            return null;
        }
        public static Frame _frameFrame(string nameOrId, Frame parentFrame)
        {
            Frame f = null;
            try
            {
                f = parentFrame.Frame(Find.ById(nameOrId).Or(Find.ByName(nameOrId)));
            }
            catch (FrameNotFoundException)
            {
                foreach (Frame frame in parentFrame.Frames)
                {
                    _log(string.Format("Details of Frame => Name :: '{0}' ID :: '{1}' ", frame.Name, frame.Id));
                    if (parentFrame.Frames.Count > 1)
                    {
                        f = _frameFrame(nameOrId, frame);
                        break;
                    }


                }
            }

            return f;
        }

        public static Frame[] _getFramesInFrame(Frame parentFrame)
        {
            Frame[] frameColl = null;
            int i = 0;
            foreach (var item in parentFrame.Frames)
            {

                frameColl[i] = item;
                i++;
            }

            return frameColl;

        }

        public static Frame[] _getFrames()
        {
            Frame[] frameColl = null;
            int i = 0;

            foreach (var item in frameColl)
            {
                frameColl[i] = item;
                i++;
                if (item.Frames.Count > 1)
                {
                    Frame[] frames = _getFramesInFrame(item);
                    foreach (var fr in frames)
                    {
                        frameColl[i] = fr;
                        i++;
                    }
                }
            }


            return frameColl;
        }


        #endregion

        #region ASSESSORS or ELEMENTS

        /// <summary>
        /// Find button with given text.
        /// </summary>
        /// <param name="nameOrIdOrTextOrLabelTextOrValue"></param>
        /// <returns>This function returns the element found.</returns>
        public static Element _button(string nameOrIdOrTextOrLabelTextOrValue)
        {
            Constraint valueCons = _get_constraint(nameOrIdOrTextOrLabelTextOrValue, "value");
            Constraint val = Find.ByValue(nameOrIdOrTextOrLabelTextOrValue);
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrValue);

            Element elm = browser.Button(allCons.Or(valueCons).Or(val));            
          
            if (elm.Exists)
            {
                _print_attributes_values(elm, new string[] { "value", "name", "id", "text" , "outerText"});
                return elm;
            }
            else
                return null;
        }

        /// <summary>
        /// Find textbox with the given name or id
        /// </summary>
        /// <param name="nameOrId"></param>
        /// <returns>This function returns the element found.</returns>
        /// 


        public static Element _textbox(string nameOrId)
        {
            Element elm = null;
            try
            {
                elm = browser.TextField(Find.ByName(nameOrId).Or(Find.ById(nameOrId)));
            }
            catch (Exception e)
            {
                Constraint allCons = _get_constraint(nameOrId);
                elm = browser.TextField(allCons);
            }

            return elm;
        }
        
        public static Frame _frameRecursiveFromBrowser(string frameIdOrName)
        {
            Frame frame = null;
            FrameCollection frames = browser.Frames;
            int frameCount = frames.Count;

            if(frameCount > 1)
            {
                foreach (var item in frames)
                {
                    frame = _getFrameFromParentFrame(frameIdOrName, item);
                    if (frame != null)
                        return frame;
                }
            }


            if(frame==null)
                _logException("Function Name: FindFrameRecursiveFromBrowser => Message :: Frame was not found.");

            return frame;
        }

        public static Frame _frameRecursiveFromParentFrame(string frameIdorName, Frame parentFrame)
        {
            Frame frame = null;
            

            if(parentFrame.Frames.Count > 1)
            {
                foreach (var item in parentFrame.Frames)
                {
                    try
                    {
                        frame = _getFrameFromParentFrame(frameIdorName, parentFrame);
                        
                    }
                    catch (Exception)
                    {
                        _log("Frame not found in frame Name ::" + parentFrame.Name + "and Id ::" + parentFrame.Id);
                    }
                    if (frame != null)
                    {
                        _log(string.Format("Given frame :: '{0}' was found in the parent frame with name :: '{1}' and id :: '{2}' .",
                            frameIdorName, parentFrame.Name, parentFrame.Id));
                        return frame;
                    }
                                         
                }
            }     
            else
            {
                try
                {
                    frame = _getFrameFromParentFrame(frameIdorName, parentFrame);
                }
                catch(Exception)
                {
                    _log("Frame not found in frame Name ::" + parentFrame.Name + "and Id ::" + parentFrame.Id);
                }
                
                if (frame != null)
                    return frame;                
  
            }

            return frame;
        }

        
        public static Frame _frame(string frameIdOrName, bool getFirstFrame)
        {
            Frame frame = null;
            int frameCount = 0;
            frameCount = browser.Frames.Count ;
            if(frameCount == 0)
            {
                _logException("Browser is not having any frames.");
                return frame;
            }
            if (frameCount == 1)
            {
                _log("Browser contains only one frame or iframe at root level.");
                if(getFirstFrame)
                    return browser.Frames.First();
            }
                
            if(frameCount > 1)
            {
                frame = _getFrameFromBrowser(frameIdOrName);
                if (frame != null)
                    return frame;
                frame = _frameRecursiveFromBrowser(frameIdOrName);
                if (frame != null)
                    return frame;
            }

            return frame;
        }




        public static Element _textbox(string nameOrId, bool searchFrames)
        {
            
            Element elm = null;
            
            if (searchFrames ==true)
            {   
                foreach (var frame in _get_browser_frames())
                {
                    _wait(500);
                    _log(string.Format("Searching in Frame :: id => '{0}' name => '{1}' ", 
                        frame.Id, frame.Name));
                    try
                    {
                        var elms = frame.TextFields.Filter(tf => tf.IdOrName == nameOrId);
                        if (elms.Count == 1)
                        {
                            elm = elms[0];
                            break;
                        }
                            
                    }
                    catch(Exception e)
                    {
                        _logException("Exception occurred :: "+ e.Message);
                    }
                        
                }
            }
            else
            {
                try
                {
                    elm = browser.TextField(Find.ByName(nameOrId).Or(Find.ById(nameOrId)));
                }
                catch (ElementNotFoundException)
                {
                    Constraint allCons = _get_constraint(nameOrId);
                    try
                    {
                        elm = browser.TextField(allCons);
                    }
                    catch (ElementNotFoundException)
                    { }
                }
            }
            //if (elm.Exists || elm != null)
            //    return elm;
            //else
            //    return null;
            return elm;
            
        }


        public static Element _textbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle, object[] relation)
        {
            Element elm =null;
            _log("RELATION INFO", string.Format("Relation Name : '{0}' and Type : '{1}'.", relation[1], relation[2]));
            
            Element nearElm = relation[0] as Element;
            elm = browser.TextField(Find.Near(nearElm.Text).Or(Find.Near(nearElm.OuterText)));
            return elm;
        }


        /// <summary>
        /// Find password field with the given name or id.
        /// </summary>
        /// <param name="nameOrId"></param>
        /// <returns>This function returns the element found.</returns>
        public static Element _password(string nameOrId)
        {
            Console.WriteLine("Using _textbox method in background.");
            return _textbox(nameOrId);
        }

        /// <summary>
        /// Find List Item with the given class name or text.
        /// </summary>
        /// <param name="nameOrIdOrTextOrLabelTextOrClassNameOrTitle"></param>
        /// <returns>This function returns the element found.</returns>
        public static Element _listItem(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.ListItem(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.ListItem(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occured :: Element not found.");
            }

        }


        public static Element _div(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
			Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.Div(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.Div(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _link(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle, bool searchFrames)
        {
            Element elm = null;
            

            if (searchFrames ==true)
            {   
                foreach (var frame in _get_browser_frames())
                {
                    _wait(500);
                    _log(string.Format("Searching in Frame :: id => '{0}' name => '{1}' ", 
                        frame.Id, frame.Name));
                    try
                    {
                        var elms = frame.Divs.Filter(a => 
                            a.IdOrName == nameOrIdOrTextOrLabelTextOrClassNameOrTitle ||
                            a.Text == nameOrIdOrTextOrLabelTextOrClassNameOrTitle ||
                            a.Title == nameOrIdOrTextOrLabelTextOrClassNameOrTitle||
                            a.ClassName == nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
                        if (elms.Count == 1)
                        {
                            elm = elms[0].Links.First();
                            break;
                        }
                            
                    }
                    catch(Exception e)
                    {
                        _logException("Exception occurred :: "+ e.Message);
                    }
                        
                }
            }
            else
            {
                elm = _link(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            }
            
            return elm;
        }

        public static Element _link(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
			Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.Link(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.Link(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                //throw new Exception("Exception Occurred :: Element not found.");
                return elm;
            }

        }

        public static Element _radio(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
			Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.RadioButton(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.RadioButton(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _checkbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
			Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.CheckBox(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.CheckBox(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _image(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
			Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.Image(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.Image(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _select(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.SelectList(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.SelectList(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _textarea(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.TextField(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.TextField(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _area(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.Area(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.Area(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }

        public static Element _form(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

            Element elm = null;
			Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            try
            {
                elm = browser.Form(allCons);
            }
            catch (WatiNException we)
            {
                Console.WriteLine("Element not found. Exception Message :: " + we.Message);
                Console.WriteLine("Waiting for 500 ms and searching again.");
                _wait(1500);
                elm = browser.Form(allCons);
            }

            if (elm.Exists)
                return elm;
            else
            {
                Console.WriteLine("Element does not exists :: " + elm.ToString());
                throw new Exception("Exception Occurred :: Element not found.");
            }

        }





        #endregion

        public static string _accessor(string evalString)
        {

            return browser.Eval(evalString);

        }

        public static Element _imageSubmitButton(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for imagesubmitbutton element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _submit(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint val = Find.ByValue(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            elm = browser.Button(allCons.Or(val));

            return elm;
        }
   

        public static Element _timebox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for timebox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _datebox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for datebox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _datetimebox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for datetimebox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _datetimelocalbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for datetimelocalbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _weekbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for weekbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _monthbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for monthbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _numberbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            // TODO: Write method for _numberbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");

            return elm;
        }
        public static Element _rangebox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for rangebox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _telephonebox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for telephonebox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _emailbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for emailbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _urlbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for urlbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _searchbox(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for searchbox HTML5 element
            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }

        public static int _random(int num)
        {
            // TODO: Write method to get random number.

            return 1;
        }
        public static void _savedRandom(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {

        }
        public static Element _cell(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            
            browser.TableCell(allCons);

            return elm;
        }
        public static Element _table(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            browser.Table(allCons);
            return elm;
        }


        public static Element _byId(string id)
        {
            Element elm = null;

            elm = browser.Element(Find.ById(id));

            return elm;
        }
        public static Element _row(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            elm = browser.TableCell(allCons);

            return elm;
        }



        public static Element _span(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            elm = browser.Span(allCons);

            return elm;
        }
        public static Element _spandiv(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for spandiv element
            if(_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }
        public static Element _option(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            // TODO: Write method for option element

            if (_check_null(elm))
                throw new NotImplementedException("Method not implemented yet.");
            return elm;
        }

        public static Element _reset(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _file(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            elm = browser.FileUpload(allCons);
            return elm;
        }

        public static Element _label(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            elm = browser.Label(allCons);
            return elm;
        }
        public static Element _byText(string elementText)
        {
            Element elm = null;

            //Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);
            elm = browser.Element(Find.ByText(elementText));

            return elm;
        }
 




        public static Document _parentNode(Element child)
        {
            Document document;
            document = child.Parent.DomContainer.NativeDocument as Document;
            return document;
        }
        public static Element _parentCell(Element elm)
        {
            TableCell cell;

            Element parent = elm.Parent;
            if (parent.TagName.ToLower() == "td")
                cell = parent as TableCell;
            else
                cell = null;
            return cell;
        }
        public static Element _parentRow(Element elm)
        {
            TableRow row;

            Element parent = elm.Parent;
            if (parent.TagName.ToLower() == "tr")
                row = parent as TableRow;
            else
                row = null;
            return row;
        }
        public static Table _parentTable(Element elm, int searchLevel = 10)
        {
            Table table = null;

            while (searchLevel > 0)
            {
                if (elm.Parent.TagName.ToLowerInvariant() == "table")
                {
                    table = elm.Parent as Table;
                    break;
                }
                else
                    table = _parentTable(elm.Parent);

                searchLevel -= 1;

            }
            
            return table;
        }

        public static object[] _in(Element elm)
        {
            return new object[] { elm, "_in", "position" };
        }

        public static object[] _near(Element elm)
        {
            return new object[] { elm, "_near", "dom" };
        }


        // TODO: TO implement below functions for HTML5 objects
        #region NOT YET IMPLEMENTED METHODS

        public static Element _rte(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }

        public static Element _iframe(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _tableHeader(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _heading1(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _heading2(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);


            return elm;
        }
        public static Element _heading3(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _heading4(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _heading5(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _heading6(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _hidden(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _title(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }



        public static Element _map(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _italic(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _bold(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _emphasis(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _strong(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;

            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _preformatted(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _code(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        public static Element _blockquote(string nameOrIdOrTextOrLabelTextOrClassNameOrTitle)
        {
            Element elm = null;
            Constraint allCons = _get_constraint(nameOrIdOrTextOrLabelTextOrClassNameOrTitle);

            return elm;
        }
        
        public static Element _xy(Element givenElm, string xc, string yc)
        {
            Element elm = null;


            return elm;
        }

        #endregion

    }
}
